package session.stateless;

import entity.Product;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

@Stateless
public class ProductBean implements ProductBeanLocal {
    @PersistenceContext()
    EntityManager em;
    
    
    boolean success = false;

    @Override
    public void createProduct(String productType, double price) {
        Product product = new Product();
        product.setProductType(productType);
        product.setProductPrice(price);
        em.persist(product);
        em.flush();
    }

    @Override
    public void updateProduct(String productType, double price) {
        Product product = em.find(Product.class, productType);
        
        if(product==null){
            System.out.println("This product type does not exist!");
        }
        else{
            product.setProductPrice(price);
            em.flush();
         }
    }

    @Override
    public void deleteProduct(String productType) {
        Product product = em.find(Product.class, productType);
        
        if(product==null){
            System.out.println("This product type does not exist!");
        }
        else{
            em.remove(product);
            }
        }

    @Override
    public List<Product> getProductList() {
        Query q=em.createQuery("SELECT c FROM Product c");
        return q.getResultList();
    }
    
}
